package com.regan.erp.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.text.StrSpliter;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 公共方法
 *
 * @author zhangby
 * @date 2018/9/24 下午4:35
 */
public class CommonUtil {

    /**
     * 创建验证码
     * @param len 验证码长度
     * @param isNumber 是否为纯数字
     * @return
     */
    public static String createAuthCode4(int len,boolean isNumber) {
        StringBuilder sb = new StringBuilder();
        String randomStr = "0123456789";
        if (!isNumber) {
            randomStr = "abcdefghijklmnopqrstuvwxyz0123456789";
        }
        List<String> randomlis = Lists.newArrayList(randomStr.split(""));
        for (int i = 0; i < len; i++) {
            sb.append(RandomUtil.randomEle(randomlis));
        }
        return sb.toString();
    }

    /**
     * 字符串模板替换 截取
     * ClassTest::getDictList4Function,{}::{} ->[ClassTest,getDictList4Function]
     *
     * @param str
     * @param temp
     * @return
     */
    public static List<String> splitStr4Temp(String str, String temp) {
        List<String> rsList = Lists.newArrayList();
        Iterator<String> iterator = Splitter.on("{}").omitEmptyStrings().split(temp).iterator();
        while (iterator.hasNext()) {
            str = str.replace(iterator.next(), "〆");
        }
        Iterator<String> split = Splitter.on("〆").omitEmptyStrings().split(str).iterator();
        while (split.hasNext()) {
            rsList.add(split.next());
        }
        return rsList;
    }

    /**
     * List 分割
     *
     * @param list list数据
     * @param star 开始
     * @param end  结束
     * @return
     */
    public static <T> List<T> splitList(List<T> list, int star, int end) {
        List li = Lists.newArrayList();
        int[] range = NumberUtil.range(star, end);
        Integer[] integers = Convert.toIntArray(range);
        Lists.newArrayList(integers).forEach(item -> li.add(list.get(item)));
        return li;
    }

    /**
     * 字符串分割
     *
     * @param Str  字符串
     * @param temp 分割字符串
     * @return
     */
    public static List<String> splitStr(String Str, String temp) {
        List<String> li = Lists.newArrayList();
        Iterator<String> split = Splitter.on(temp).omitEmptyStrings().trimResults().split(Str).iterator();
        while (split.hasNext()) {
            li.add(split.next());
        }
        return li;
    }

    /**
     * url截掉无用参数
     *
     * @param url       url地址
     * @param paramName 参数名称
     * @return
     */
    public static String curtParam4Url(String url, String paramName) {
        return editParam4Url(url, map -> map.remove(paramName));
    }

    /**
     * url截掉添加参数
     *
     * @return
     */
    public static String editParam4Url(String url, Consumer<Map<String, List<String>>> consumer) {
        //实例化url参数
        Map<String, List<String>> paramsMap = HttpUtil.decodeParams(url, "utf-8");
        consumer.accept(paramsMap);
        //转换参数
        String params = HttpUtil.toParams(paramsMap);
        //截取真是url
        String currentUrl = StrSpliter.splitTrim(url, "?", true).get(0);
        return currentUrl + "?" + params;
    }


    /**
     * 读取cvs
     *
     * @param filepath cvs路径
     * @return
     */
    public static List<List<String>> readCvs(String filepath) {
        List<String> allString = Lists.newArrayList();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(filepath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String line = "";
        String everyLine = "";
        try {
            while ((line = br.readLine()) != null)  //读取到的内容给line变量
            {
                everyLine = line;
                allString.add(everyLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        //数据处理
        List<List<String>> rsList = Lists.newArrayList();
        allString.forEach(str -> {
            List<String> collect = CommonUtil.splitStr(str, ",").stream()
                    .map(s -> s.replace("`", ""))
                    .collect(Collectors.toList());
            rsList.add(collect);
        });
        return rsList;
    }

    /**
     * 读取cvs文件
     *
     * @param filepath 路径
     * @param star     开始
     * @param end      结束
     * @return
     */
    public static List<List<String>> readCvs(String filepath, int star, int end) {
        return splitList(readCvs(filepath), star, end);
    }

    /**
     * 读取cvs文件
     *
     * @return
     */
    public static List<List<String>> readCvs(String filepath, int star, Function<List, Integer> func) {
        List<List<String>> lists = readCvs(filepath);
        return splitList(lists, star, func.apply(lists));
    }

    /**
     * 数字转换
     * 999 -> 999
     * 1000 -> 1.0k
     * 12900 -> 12.9k
     * 129000 -> 12.9w
     * 12900000 -> 1.2kw
     * @param num
     * @return
     */
    public static String num2Kw(Integer num) {
        String rsNum = num.toString();
        if (num >= 1000 && num < 100000) {
            rsNum = new BigDecimal(num / 1000.0).setScale(1, BigDecimal.ROUND_FLOOR).toString() + "k";
        } else if (num >= 100000 && num < 10000000) {
            rsNum = new BigDecimal(num / 10000.0).setScale(1, BigDecimal.ROUND_FLOOR).toString() + "w";
        } else if (num >= 10000000) {
            rsNum = new BigDecimal(num / 10000000.0).setScale(1, BigDecimal.ROUND_FLOOR).toString() + "kw";
        }
        return rsNum;
    }
}
